import React, { PureComponent, Fragment } from 'react';
import { connect } from 'react-redux';
import { Switch, Route, withRouter } from 'react-router';
// import { Redirect } from 'react-router-dom';
import Loadable from 'react-loadable';

import Loading from '../components/common/lazyLoading';
import { getQueryParam } from '../utils/url';
import withNav from '../components/nav/nav.escort';
import { AUTH_ENTRY } from '../constants/setting';

export const basePath = '/escort';

export const routes = [
  {
    path: '/agreement',
    needLogin: false,
    component: Loadable({
      loader: () => import('../pages/app/agreement.escort'),
      loading: Loading,
    }),
  },
  {
    path: '/rule',
    needLogin: false,
    component: Loadable({
      loader: () => import('../pages/app/rule.escort'),
      loading: Loading,
    }),
  },
  {
    path: '/login',
    needLogin: false,
    component: Loadable({
      loader: () => import('../pages/user/login.escort'),
      loading: Loading,
    }),
  },
  {
    path: '/couponList',
    needLogin: true,
    component: withNav(
      Loadable({
        loader: () => import('../pages/coupon/couponList.escort'),
        loading: Loading,
      }),
      2,
    ),
  },
  {
    path: '/giftCenter',
    needLogin: true,
    component: withNav(
      Loadable({
        loader: () => import('../pages/user/giftCenter.escort'),
        loading: Loading,
      }),
      1,
    ),
  },
  {
    path: '/menberPackage',
    needLogin: true,
    component: Loadable({
      loader: () => import('../pages/user/menberPackage'),
      loading: Loading,
    }),
  },
  {
    path: '/useableCouponList',
    needLogin: true,
    component: withNav(
      Loadable({
        loader: () => import('../pages/coupon/couponList.escort'),
        loading: Loading,
      }),
      0,
      { type: 'useable' },
    ),
  },
  {
    path: '/result',
    needLogin: true,
    component: Loadable({
      loader: () => import('../pages/result/result.escort'),
      loading: Loading,
    }),
  },
  {
    path: '/couponCode',
    needLogin: true,
    component: Loadable({
      loader: () => import('../pages/code/payCode.card.escort'),
      loading: Loading,
    }),
  },
  {
    path: '/addInsuranceInfo',
    needLogin: true,
    component: Loadable({
      loader: () => import('../pages/insurance/addInsuranceInfo'),
      loading: Loading,
    }),
  },
  {
    path: '*',
    component: Loadable({
      loader: () => import('../pages/app/404'),
      loading: Loading,
    }),
  },
];

@withRouter
@connect(({ user, coupon, loading }) => {
  return {
    user: user.get('user'),
    showDialog: coupon.getIn(['dialog', 'show']),
    dialogType: coupon.getIn(['dialog', 'type']),
    needCarNo: coupon.getIn(['dialog', 'needCarNo']),
    giftBusy: ['coupon/getGiftPackage'].some(action => loading.effects[action]),
  };
})
class EscortRoutes extends PureComponent {
  state = {
    showNav: true,
  }
  componentDidMount() {
    // window.source = 'CHONGQINGPUBLIC';
    window.source = 'GDBJYY';
  }


  render() {
    const { showNav } = this.state;
    const {
      user,
    } = this.props;
    const cells = routes.map((route) => {
      const {
        path,
        component: AppComponent,
        needLogin,
        ...rest
      } = route;
      const relPath = `${basePath}${path}`;
      console.log('=========', path);
      return (
        <Route
          key={relPath}
          path={relPath}
          {...rest}
          render={(props) => { // eslint-disable-line
            if (needLogin && !user.get('token') && !getQueryParam('yyeCode') && path !== '/login') {
              window.location.href = `${AUTH_ENTRY}?industryId=40&callBackUrl=${encodeURIComponent(window.location.href)}`;
              return <div />;
            } else {
              return <AppComponent showNav={showNav} {...props} canRefuel />;
            }
            // return <AppComponent showNav={showNav} {...props} canRefuel />;
          }}
        />
      );
    });
    return (
      <Fragment>
        <Switch>
          {cells}
        </Switch>
      </Fragment>

    );
  }
}

export default EscortRoutes;

